package cn.zjh.base.controller;

import cn.zjh.base.exception.BusinessException;
import cn.zjh.user.service.service.IVerifyCodeService;
import cn.zjh.base.utils.AjaxResult;
import cn.zjh.base.vo.RegisterVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService;

    /**
    * @Title: graph
    * @Description: 图形验证码
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/6 16:24
    * @Parameters:
    * @Return
    */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult graph(@PathVariable("imageCodeKey") String imageCodeKey){
        try {
            //获取图形验证码的base64字符串
            String base64 = verifyCodeService.graph(imageCodeKey);
            //放入响应中进行返回
            return AjaxResult.me().setResultObj(base64);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: smsCode
    * @Description: 短信验证码
    * @Author: Mr.Zhang
    * @Version: 1.0
    * @Date:  2022/7/9 16:11
    * @Parameters:
    * @Return
    */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterVo vo){
        try {
            //发送短信
            verifyCodeService.smsCode(vo);
            //放入响应中进行返回
            return AjaxResult.me();
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，请重试！");
        }
    }



}
